package org.mspring.mlog.service;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.mspring.mlog.dao.PhotoDao;
import org.mspring.mlog.entity.Photo;
import org.mspring.mlog.service.exception.StorageException;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.mspring.platform.utils.ImageUtils;
import org.mspring.platform.utils.Size;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author Gao Youbo
 * @since 2012-10-17
 */
@Service
public class PhotoService {

    private static final Logger log = LoggerFactory.getLogger(PhotoService.class);

    @Autowired
    private PhotoDao photoDao;

    @Autowired
    private StorageService storageService;

    @Autowired
    private PhotoUploadService photoUploadService;

    public Photo findById(long id) {
        return photoDao.findById(id);
    }

    public Page<Photo> listPage(QueryBuilder queryBuilder, Page<Photo> page) {
        return photoDao.listPage(queryBuilder, page);
    }

    @Transactional
    public Long createPhoto(HttpServletRequest request, Long album) throws IOException, StorageException {
        MultipartFile mf = photoUploadService.getPhotoMultipartFile(request);
        BufferedImage image = photoUploadService.getBufferedImage(mf);
        if (photoUploadService.isLimitSize(image.getWidth(), image.getHeight())) {
            Size maxSize = photoUploadService.getMaxSize();
            Size size = photoUploadService.getZoomSize(image.getWidth(), image.getHeight(), (int) maxSize.getWidth(), (int) maxSize.getHeight());
            image = ImageUtils.resize(image, (int) size.getWidth(), (int) size.getHeight());
        }
        String photoSaveName = photoUploadService.getPhotoSaveName(mf);
        String photoSavePath = photoUploadService.getPhotoSavePath(photoSaveName);
        String url = photoUploadService.uploadPhoto(image, photoSavePath);

        String photoPreviewSavenPath = photoUploadService.getPhotoPreviewSavePath(photoSavePath);
        Size previewSize = photoUploadService.getZoomSize(image.getWidth(), image.getHeight(), photoUploadService.DEFAULT_MAX_PREVIEW_WIDTH, photoUploadService.DEFAULT_MAX_PREVIEW_HEIGHT);
        BufferedImage previewImage = ImageUtils.resize(image, (int) previewSize.getWidth(), (int) previewSize.getHeight());
        String previewUrl = photoUploadService.uploadPhoto(previewImage, photoPreviewSavenPath);

        Photo photo = photoUploadService.getPhotoEntity(image, url, album, mf, photoSaveName, previewUrl);
        return photoDao.create(photo);
    }

    @Transactional
    public void deletePhoto(Long... id) {
        if (id == null) {
            return;
        }
        Photo photo = null;
        for (Long i : id) {
            photo = findById(i);
            try {
                storageService.remove(photo.getUrl());
                storageService.remove(photo.getPreviewUrl());
            } catch (Exception e) {
                continue;
            }
            // 如果有相册使用该图片为封面，那么取消相册封面
            // executeUpdate("update Album album set album.cover.id = null where album.cover.id = ?",
            // i);
            photoDao.delete(photo);
        }
    }

    @Transactional
    public void updatePhoto(Photo photo) {
        photo.setModifyTime(new Date());
        photoDao.update(photo);
    }
}
